<?php
/*
 * @Date: 2023-04-02 14:27:54
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2023-04-04 15:42:41
 * @FilePath: \jsadmin\app\common\command\Redis.php
 */
namespace app\common\command;

use app\admin\model\SystemAdmin;
use app\api\model\SupplyOrder;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\api\model\SupplyOrders;
use think\facade\Env;

class Redis extends Command
{
    protected function configure()
    {
        $this->setName('redis')
            ->setDescription('redis监听');
    }
    protected function execute(Input $input, Output $output)
    {
        // 定义不超时 一直运行
        ini_set('default_socket_timeout', -1);
        $redis = new \Redis();
        $redis->connect(Env::get('redis.host'),Env::get('redis.port'));
        $redis->auth(Env::get('redis.password'));
        $redis->psubscribe(["__keyevent@0__:expired"], function($rediss, $pattern, $chan, $msg) use($output) {
            try {
                // 处理业务逻辑
                if(preg_match("/^JS*/",$msg)){
                    $use = SupplyOrders::where('order_number',$msg)->find();
                    if($use['status'] == '1'){
                        $order = new SupplyOrder();
                        $select = SystemAdmin::select();
                        foreach ($select as $k => $v) {
                            if ($v['status'] == 0) continue;
                            $app = [
                                'name' => "$msg",
                                'status' => '等待处理'
                            ];
                            $order->supplyor($v['open_id'],$app);
                            $output->writeln("已经帮您催单，请耐心等待");
                        } 
                    }
                    throw new \Exception('订单已经处理请重新查看订单');
                }
            } catch (\Exception $e) {
                $output->writeln($e->getMessage());
            }
        });
    }
}